package org.tczs93.commonreadwrite.config;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：tao.li
 * @description：
 * @date ：Created in 2021/8/23 15:42
 */
@Configuration
public class DataSourceConfig{
    protected static final Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);


    @Value("${spring.datasource.type}")
    public Class<? extends DataSource> dataSourceType;


    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDataSource() {
        DataSource masterDataSource = DataSourceBuilder.create().type(dataSourceType).build();
        logger.info("master--------------");
        return masterDataSource;
    }

    @Bean(name = "slave1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.slave1")
    public DataSource slave1DataSource() {
        DataSource slave1DataSource = DataSourceBuilder.create().type(dataSourceType).build();
        logger.info("slave--------------");
        return slave1DataSource;
    }

    @Bean(name = "customerDataSource")
    public DataSource customerDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
                                        @Qualifier("slave1DataSource") DataSource slave1DataSource) {
        CustomerDataSource customerDataSource = new CustomerDataSource();
        customerDataSource.setMasterDataSource(masterDataSource);
        customerDataSource.setSlave1DataSource(slave1DataSource);
        return customerDataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(@Qualifier("customerDataSource") DataSource customerDataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(customerDataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mybatis/mapper/**.xml"));
        sqlSessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver()
                .getResource("classpath:mybatis/mybatis-config.xml"));
        sqlSessionFactoryBean.setPlugins(new RouteDataSourcePlugin());
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager platformTransactionManager(@Qualifier("masterDataSource") DataSource masterDataSource) {
        return new DataSourceTransactionManager(masterDataSource);
    }

}
